﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using DataBaseConnection;
using DataEntity;

namespace CMSLibrary
{
    /// <summary>
    /// 所有子类公用的方法
    /// </summary>
    public class PublicMethod : PDispose
    {
        /// <summary>
        /// 返回安装目录
        /// </summary>
        protected string GetInstallDirectory
        {
            get
            {
                return UIMethod.GetAppSettings("InstallDir");
            }
        }
        /// <summary>
        /// 获取本地数据源
        /// </summary>
        /// <param name="_Connect">连接字符串</param>
        /// <param name="paras_out">参数集合</param>
        /// <param name="_SQLText">查询语句</param>
        /// <param name="_DBType">数据库类型</param>
        /// <returns>数据集</returns>
        public static DataSet GetLocalSourceToTry(string _Connect, string[] paras_out, string _SQLText, int _DBType, int MaxRows, int CurrPage)
        {
            DataSet result = new DataSet();
            DataBaseType dbt = (DataBaseType)_DBType;
            
            PConnOrder co = CSelectDataBase.GetDataBase(dbt, _Connect);            

            try
            {
                if (paras_out != null && paras_out.Length != 0)
                {
                    _SQLText = string.Format(_SQLText, paras_out);                    
                }

                if (MaxRows == 0)
                {
                    result = co.ExecuteDataSet(_SQLText, null, CommandType.Text);
                }
                else 
                {
                    result = co.SplitPage(_SQLText, string.Empty, string.Empty, MaxRows, CurrPage);
                }
                
                co.ConnectionClose();
            }
            catch(Exception ex)
            {
                co.ConnectionClose();
                throw ex;
            }            
            return result;
        }

        public static DataSet GetLocalSource(string _Connect, string[] paras_out, string _SQLText, int _DBType, int MaxRows, int CurrPage)
        {
            try
            {
                return GetLocalSourceToTry(_Connect, paras_out, _SQLText, _DBType, MaxRows, CurrPage);
            }
            catch { return null; }
        }

        /// <summary>
        /// 获取数据源(ACCESS可以直接分页)
        /// </summary>
        /// <param name="sourceid">数据源编号</param>
        /// <param name="paras">外部参数</param>
        /// <returns></returns>
        public static DataSet GetSource(int sourceid, string[] params_values, int MaxRows, int CurrPage)
        {
            DataSet ds = null;  //数据源           

            tab_DataSource fcds = (tab_DataSource)new Cache_DataSource().Cache_getData(sourceid);
            if (!fcds.IsEmpty)
            {
                DataBaseType dbt = DataBaseType.ACCESS;
                if (fcds.src_IsConfig.Value)
                {
                    fcds.src_ConnectionString = CSelectDataBase.GetConnectionStrings("DefaultConnection", ref dbt);
                    fcds.src_DataBaseType = (int)dbt;
                }

                ds = GetLocalSource(
                    fcds.src_ConnectionString, 
                    params_values, 
                    fcds.src_SelectText, 
                    fcds.src_DataBaseType.Value,
                    MaxRows, 
                    CurrPage);
            }

            return ds;
        }

    }
}